/*
 * Copyright (c) 2010-2011 Josef Hardi <josef.hardi@gmail.com>
 * Copyright (c) 2010 Piotr Zagawa <piotr.zagawa@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package id.duapuluhdua.dakidaki.ui;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;

import id.duapuluhdua.dakidaki.common.CommonActivity;
import id.duapuluhdua.dakidaki.map.Screen;

/**
 *
 */
public class StatusMessage extends UIElement
{
  /** Fields */
  private static StatusMessage message;

  private Paint mBackground;
  private Paint mText;
  private Rect mRtText;
  private String mTextString;
  private int mTextHeight;

  private boolean bVisible;

  public StatusMessage(CommonActivity parent, Screen screen)
  {
    super(parent, screen);

    message = this;

    mTextString = "INITIAL";

    mRtText = new Rect(0, 0, 0, 0);
    mTextHeight = mRtText.height();

    mText = new Paint();
    mText.setAntiAlias(true);
    mText.setColor(mStyle.textColor1);
    mText.setTextSize(mStyle.textSize1);
    mText.getTextBounds(mTextString, 0, mTextString.length(), mRtText);

    mBackground = new Paint();
    mBackground.setAntiAlias(true);
    mBackground.setColor(mStyle.backgroundColor);
  }

  @Override
  public void updateStyle()
  {
    mStyle.backgroundColor = 0xff306070;
    mStyle.textColor1 = 0xffffffff;
    mStyle.textSize1 = 16;
    mStyle.backgroundMargin = 6;
    mStyle.backgroundRound = 6;
  }

  @Override
  public void doDraw(Canvas canvas)
  {
    if (bVisible) {
      super.doDraw(canvas);

      this.drawSimpleShadow(canvas, 8);

      // Draw background
      canvas.drawRoundRect(mRtBackground, mStyle.backgroundRound, mStyle.backgroundRound, mBackground);

      // Draw text
      canvas.drawText(mTextString, mRtBounds.left, mRtBounds.top + mTextHeight, mText);
    }
  }

  public static void hide()
  {
    message.bVisible = false;
    message.mTextString = "";
  }

  public static void showText(String text)
  {
    synchronized (message.mTextString)
    {
      message.bVisible = false;
      message.mTextString = text;

      final int margin = message.mStyle.buttonMargin + (int)message.mStyle.backgroundMargin;
      final int width = UIElement.screenWidth - (margin * 2);
      final int height = message.mTextHeight + (int)message.mStyle.backgroundMargin;
      final int left = margin;
      final int top = screenHeight - height;

      message.setSize(width, height);
      message.setPos(left, top);

      message.bVisible = true;
    }
  }

  @Override
  public void surfaceSizeChanged(int width, int height, int screenOrientation)
  {
    // Do nothing.
  }

  @Override
  public void onClick()
  {
    // Do nothing.
  }
}
